package com.example.dameng.controller;

import com.example.dameng.api.CommonResult;
import com.example.dameng.service.UserService;
import com.example.dameng.utils.SecretUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@ResponseBody
@RestController
public class LoginController {
    @Autowired
    UserService userService;

    @GetMapping(value = "/login")
    public CommonResult adminLogin(@RequestParam int userCode,@RequestParam String userPasswd,@RequestParam String role) {

        //输出测试
        System.out.println("---登录测试---");
        System.out.println(userCode);
        System.out.println(userPasswd);
        System.out.println(role);

        Integer account;
        if(Objects.equals(role, "普通用户")){
            account=userService.userLogin(userCode,SecretUtil.desEncrypt(userPasswd));
            System.out.println(account);
        }
        else{
            if(Objects.equals(role, "总局")){
                 account = userService.adminLogin(userCode,SecretUtil.desEncrypt(userPasswd),1);
            }
            else {
                 account = userService.adminLogin(userCode,SecretUtil.desEncrypt(userPasswd),2);
            }

        }
        if(account!=null){
            return CommonResult.success(account,"登录成功！");
        }
        else{
            return CommonResult.failed("登录失败！");
        }
        //user=userService.Login(username, SecretUtil.desEncrypt(password));
    }
}
